﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL.Entity;
using DAL.Persistencia;

namespace NewTierra.Pages.Servicos
{
    public partial class AddSupplierTarifas : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }

        }

        #region Popula Dados

        protected void PopulaGridMServicos()
        {
            try
            {
                MontaServicoDAL ms = new MontaServicoDAL();
                SupplierDAL sd = new SupplierDAL();

                Supplier s = sd.FindAllNome(txtSupplier.Text);

                DateTime From = Convert.ToDateTime(Request.QueryString["from"]);
                DateTime To = Convert.ToDateTime(Request.QueryString["to"]);

                GridMServicos.DataSource = ms.ListarTodosCondicaoMServicoSub(s.S_id, From, To, Convert.ToInt32(rdoBasesIndexSubServico.SelectedValue));
                GridMServicos.DataBind();

            }
            catch (Exception ex)
            {
                lblAddSupplierTarifas.Text = ex.Message;
            }
        }

        #endregion

        #region Regras

        [WebMethod]
        public static List<string> SearchSuppliers(string prefixText, int count)
        {
            try
            {
                SupplierDAL s = new SupplierDAL();

                List<Supplier> supp = s.ListarTodos(prefixText);

                List<string> lista = new List<string>();

                if (supp.Count == 0)
                {
                    lista.Add("Não existe supplier cadastrado com esse nome.");

                    return lista;
                }

                foreach (Supplier p in supp)
                {
                    lista.Add(p.S_nome);
                }

                return lista;
            }
            catch
            {
                throw;
            }
        }

        protected void btnFiltroNomeSupplier_Click(object sender, EventArgs e)
        {
            PopulaGridMServicos();
        }

        protected void EditarMServico(object sender, CommandEventArgs e)
        {
            try
            {
                int myid = Convert.ToInt32(e.CommandArgument);

                ServicoCompletoDAL scd = new ServicoCompletoDAL();
                MontaServicoValoresDAL msd = new MontaServicoValoresDAL();
                Servico_Completo sc = new Servico_Completo();

                if (scd.VerificaMServicoTemp(myid, Convert.ToInt32(Request.QueryString["idMS"])))
                {
                    MostrarMsg("Este Subserviço já foi adicionado a este serviço.");
                    return;
                }

                sc.IdMServico = myid;
                sc.Servicos_Id = Convert.ToInt32(Session["IdServico"]);
                sc.IdMServico_Temporada = Convert.ToInt32(Request.QueryString["idMS"]);
                sc.Base_Index = Convert.ToInt32(rdoBasesIndexSubServico.SelectedValue);

                scd.Salvar(sc);


                MostrarMsg("Subserviço adicionado com sucesso.");

            }
            catch (Exception ex)
            {
                lblAddSupplierTarifas.Text = ex.Message;
            }

            //FecharJanela();
            //this.Page.ClientScript.RegisterStartupScript(typeof(Page), "closeWindow", "<script type='text/javascript'>self.opener=null; self.close();</script>");
        }

        protected void FecharJanela()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            sb.Append("function RefreshParent() {");
            sb.Append("window.opener.document.getElementById('btnHidden').click();");
            sb.Append("window.close(); }");

            //ScriptManager.RegisterStartupScript(Me, Me.GetType(), "myscript", sb.ToString(), True)
            ScriptManager.RegisterStartupScript(this, typeof(string), "popup", sb.ToString(), true);
        }

        protected void rdoBasesIndexSubServico_SelectedIndexChanged(object sender, EventArgs e)
        {
            PopulaGridMServicos();
        }

        #endregion

        protected void MostrarMsg(string Msg)
        {
            ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "call me", "showAutoCloseMessage('" + Msg + "')", true);
        }        

    }
}